package com.jdz.gxq.info.admin.trade.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.trade.bo.Trade;
import com.jdz.gxq.info.admin.trade.form.EditTradeForm;
import com.jdz.gxq.info.admin.trade.service.TradeService;
import com.jdz.gxq.info.common.action.CommonAction;

public class SaveTradeAction extends CommonAction {

	private TradeService tradeService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		EditTradeForm editTradeForm = (EditTradeForm) form;
		
		Trade trade = editTradeForm.getTrade();
		
		// trade名重复验证(其他后台逻辑验证也类似处理)
		if (!tradeService.checkTradeName(trade, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("trade.enterpriseName", new ActionMessage("error.tradeNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		trade.setModifiedBy(getUserId(request));
		
		tradeService.updateTrade(trade);
		
		return mapping.findForward("success");
	}

	public void setTradeService(TradeService tradeService) {
		this.tradeService = tradeService;
	}
}

